
package com.zcp.pcsuitall;

import java.util.Random;

import javax.crypto.spec.IvParameterSpec;

public class MyDes {

    private static byte[] iv = {
            1, 2, 3, 4, 5, 9, 7, 8
    };

    private static IvParameterSpec deZeroIv = new IvParameterSpec(iv);

    /**
     * 解密，注意密码只能是8位。
     * 
     * @param decrypeString
     * @param decrypetKey
     * @return
     * @throws Exception
     */
    public static String decryptDES(String decrypeString, String decrypetKey) {
        return decrypeString;
        // byte[] decryptedData = null;
        // try {
        // byte[] byteMi = Base64.decodeBase64(decrypeString);
        // SecretKeySpec key = new SecretKeySpec(decrypetKey.getBytes(), "DES");
        // Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        // cipher.init(Cipher.DECRYPT_MODE, key, deZeroIv);
        // decryptedData = cipher.doFinal(byteMi);
        // } catch (Exception e) {
        // // e.printStackTrace();
        // }
        // return new String(decryptedData);
    }

    /**
     * 加密，注意密码只能是8位,算法是DES,模式是CBC,填充是PKCS5Padding。
     * 
     * @param encrypeString
     * @param encrypetKey
     * @return
     * @throws Exception
     */
    public static String encryptDES(String encrypeString, String encrypetKey) {
        // byte[] encryptedData = null;
        // try {
        // IvParameterSpec zeroIv = new IvParameterSpec(iv);
        // SecretKeySpec key = new SecretKeySpec(encrypetKey.getBytes(), "DES");
        // Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        // cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
        // encryptedData = cipher.doFinal(encrypeString.getBytes());
        //
        // } catch (Exception e) {
        // // e.printStackTrace();
        // }
        // return Base64.encodeBase64String(encryptedData);
        return encrypeString;
    }

    private static char[] src = "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"
            .toCharArray();

    /**
     * 获取随机字符串。
     * 
     * @param length
     * @return
     */
    public static String getRandomStr(int length) {
        if (length < 1) {
            return null;
        }
        Random random = new Random();
        char[] buf = new char[length];
        int index = 0;
        for (int i = 0; i < length; i++) {
            index = random.nextInt(src.length);
            buf[i] = src[index];
        }
        return new String(buf);
    }

}
